package com.example.goodpet.my.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.example.goodpet.R;
import com.example.goodpet.activity.MainActivity;
import com.example.goodpet.activity.WebViewActivity;
import com.example.goodpet.utils.ActivityManager;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.GlideCacheUtil;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.UIUtils;
import com.zackratos.ultimatebarx.ultimatebarx.java.UltimateBarX;

public class SettingActivity extends FragmentActivity implements View.OnClickListener {
    private Button btnLogout;
    private Button btnAgreement;
    private Button btnPrivacy;
    private Button btnClear;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        UltimateBarX.statusBarOnly(this)
                .fitWindow(true)
                .colorRes(R.color.ic_back_background)
                .light(true)
                .apply();
        ActivityManager.getInstance().addActivity(this);

        btnLogout=findViewById(R.id.btn_logout);
        btnAgreement=findViewById(R.id.btn_agreement);
        btnPrivacy=findViewById(R.id.btn_privacy);
        btnClear=findViewById(R.id.btn_clear_cache);



        btnLogout.setOnClickListener(this);
        btnAgreement.setOnClickListener(this);
        btnPrivacy.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.btn_logout){
            SharedPreferences userInfo = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
            userInfo.edit().clear().apply();
            ActivityManager.getInstance().removeAll();
            MyApplication.user=null;
            startActivity(new Intent(UIUtils.getContext(), MainActivity.class));
        }else if (v.getId()==R.id.btn_agreement){
            WebViewActivity.actionStart(this, Constants.BASE_URL_support+"/agreement.html");
        }else if (v.getId()==R.id.btn_privacy){
            WebViewActivity.actionStart(this, Constants.BASE_URL_support+"/privacy.html");
        }else if (v.getId()==R.id.btn_clear_cache){
            String cacheSize = GlideCacheUtil.getInstance().getCacheSize(UIUtils.getContext());
            GlideCacheUtil.getInstance().clearImageAllCache(UIUtils.getContext());
            UIUtils.toast("节省了"+cacheSize+"的空间",false);
        }

    }
}